# for debugging use
# generate the first bin and its md5

import csv
import re

# from common import bin_storage


# split the latest into two
def item_splitter(d):
    latest = d.pop('latest')
    if latest == "":
        gong = ""
        wei = ""
    elif latest == "EMPTY":
        gong = "EMPTY"
        wei = "EMPTY"
    else:
        gong, wei = latest.split('\n')
    d['gong'] = gong
    d['wei'] = wei
    return d

def run(ana_path):
    f = csv.DictReader(open(ana_path, encoding="utf-8-sig", errors="ignore"))
    full_set = {}
    for row in f:
        d = dict(row)
        code = d.pop('code')
        full_set[code] = item_splitter(d)
    for k in full_set:
        v = full_set[k]
        f1_time = v.pop('f1_time')
        f4_time = v.pop('f4_time')
        for i in v:
            if i == 'shareholder':
                v[i] = [v[i] + ' @{:}'.format(f4_time)]
            else:
                v[i] = [v[i] + ' @{:}'.format(f1_time)]

    return ana_path, full_set


def save(params):
    ana_path, full_set = params
    name = re.search('ana_([\d]{4}-[\d]{2}-[\d]{2})\.csv', ana_path).group(1)
    # bin_storage.Bin_storage().save(name, full_set)


if __name__ == '__main__':
    print(run('../f10/ana/ana_2020-07-02.csv')[1])
